home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / gcc270_src.lha / gcc-2.7.0-amiga / config / fp-bit.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  28KB  |  1,291 lines

  1. /* This is a software floating point library which can be used instead of
  2.    the floating point routines in libgcc1.c for targets without hardware
  3.    floating point.  */
  4.  
  5. /* Copyright (C) 1994 Free Software Foundation, Inc.
  6.  
  7. This file is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. In addition to the permissions in the GNU General Public License, the
  13. Free Software Foundation gives you unlimited permission to link the
  14. compiled version of this file with other programs, and to distribute
  15. those programs without any restriction coming from the use of this
  16. file.  (The General Public License restrictions do apply in other
  17. respects; for example, they cover modification of the file, and
  18. distribution when not linked into another program.)
  19.  
  20. This file is distributed in the hope that it will be useful, but
  21. WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  23. General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program; see the file COPYING.  If not, write to
  27. the Free Software Foundation, 59 Temple Place - Suite 330,
  28. Boston, MA 02111-1307, USA.  */
  29.  
  30. /* As a special exception, if you link this library with other files,
  31.    some of which are compiled with GCC, to produce an executable,
  32.    this library does not by itself cause the resulting executable
  33.    to be covered by the GNU General Public License.
  34.    This exception does not however invalidate any other reasons why
  35.    the executable file might be covered by the GNU General Public License.  */
  36.  
  37. /* This implements IEEE 754 format arithmetic, but does not provide a
  38.    mechanism for setting the rounding mode, or for generating or handling
  39.    exceptions.
  40.  
  41.    The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
  42.    Wilson, all of Cygnus Support.  */
  43.  
  44. /* The intended way to use this file is to make two copies, add `#define FLOAT'
  45.    to one copy, then compile both copies and add them to libgcc.a.  */
  46.  
  47. /* The following macros can be defined to change the behaviour of this file:
  48.    FLOAT: Implement a `float', aka SFmode, fp library.  If this is not
  49.      defined, then this file implements a `double', aka DFmode, fp library.
  50.    FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
  51.      don't include float->double conversion which requires the double library.
  52.      This is useful only for machines which can't support doubles, e.g. some
  53.      8-bit processors.
  54.    CMPtype: Specify the type that floating point compares should return.
  55.      This defaults to SItype, aka int.
  56.    US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
  57.      US Software goFast library.  If this is not defined, the entry points use
  58.      the same names as libgcc1.c.
  59.    _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
  60.      two integers to the FLO_union_type.  
  61.    NO_NANS: Disable nan and infinity handling
  62.    SMALL_MACHINE: Useful when operations on QIs and HIs are faster
  63.      than on an SI */
  64.  
  65. typedef SFtype __attribute__ ((mode (SF)));
  66. typedef DFtype __attribute__ ((mode (DF)));
  67.  
  68. typedef int HItype __attribute__ ((mode (HI)));
  69. typedef int SItype __attribute__ ((mode (SI)));
  70. typedef int DItype __attribute__ ((mode (DI)));
  71.  
  72. /* The type of the result of a fp compare */
  73. #ifndef CMPtype
  74. #define CMPtype SItype
  75. #endif
  76.  
  77. typedef unsigned int UHItype __attribute__ ((mode (HI)));
  78. typedef unsigned int USItype __attribute__ ((mode (SI)));
  79. typedef unsigned int UDItype __attribute__ ((mode (DI)));
  80.  
  81. #define MAX_SI_INT   ((SItype) ((unsigned) (~0)>>1))
  82. #define MAX_USI_INT  ((USItype) ~0)
  83.  
  84.  
  85. #ifdef FLOAT_ONLY
  86. #define NO_DI_MODE
  87. #endif
  88.  
  89. #ifdef FLOAT
  90. #    define NGARDS    7L
  91. #    define GARDROUND 0x3f
  92. #    define GARDMASK  0x7f
  93. #    define GARDMSB   0x40
  94. #    define EXPBITS 8
  95. #    define EXPBIAS 127
  96. #    define FRACBITS 23
  97. #    define EXPMAX (0xff)
  98. #    define QUIET_NAN 0x100000L
  99. #    define FRAC_NBITS 32
  100. #    define FRACHIGH  0x80000000L
  101. #    define FRACHIGH2 0xc0000000L
  102.     typedef USItype fractype;
  103.     typedef UHItype halffractype;
  104.     typedef SFtype FLO_type;
  105.     typedef SItype intfrac;
  106.  
  107. #else
  108. #    define PREFIXFPDP dp
  109. #    define PREFIXSFDF df
  110. #    define NGARDS 8L
  111. #    define GARDROUND 0x7f
  112. #    define GARDMASK  0xff
  113. #    define GARDMSB   0x80
  114. #    define EXPBITS 11
  115. #    define EXPBIAS 1023
  116. #    define FRACBITS 52
  117. #    define EXPMAX (0x7ff)
  118. #    define QUIET_NAN 0x8000000000000LL
  119. #    define FRAC_NBITS 64
  120. #    define FRACHIGH  0x8000000000000000LL
  121. #    define FRACHIGH2 0xc000000000000000LL
  122.     typedef UDItype fractype;
  123.     typedef USItype halffractype;
  124.     typedef DFtype FLO_type;
  125.     typedef DItype intfrac;
  126. #endif
  127.  
  128. #ifdef US_SOFTWARE_GOFAST
  129. #    ifdef FLOAT
  130. #        define add         fpadd
  131. #        define sub         fpsub
  132. #        define multiply     fpmul
  133. #        define divide         fpdiv
  134. #        define compare         fpcmp
  135. #        define si_to_float     sitofp
  136. #        define float_to_si     fptosi
  137. #        define float_to_usi     fptoui
  138. #        define negate         __negsf2
  139. #        define sf_to_df        fptodp
  140. #        define dptofp         dptofp
  141. #else
  142. #        define add         dpadd
  143. #        define sub         dpsub
  144. #        define multiply     dpmul
  145. #        define divide         dpdiv
  146. #        define compare         dpcmp
  147. #        define si_to_float     litodp
  148. #        define float_to_si     dptoli
  149. #        define float_to_usi     dptoul
  150. #        define negate         __negdf2
  151. #        define df_to_sf     dptofp
  152. #endif
  153. #else
  154. #    ifdef FLOAT
  155. #        define add         __addsf3
  156. #        define sub         __subsf3
  157. #        define multiply     __mulsf3
  158. #        define divide         __divsf3
  159. #        define compare         __cmpsf2
  160. #        define _eq_f2         __eqsf2
  161. #        define _ne_f2         __nesf2
  162. #        define _gt_f2         __gtsf2
  163. #        define _ge_f2         __gesf2
  164. #        define _lt_f2         __ltsf2
  165. #        define _le_f2         __lesf2
  166. #        define si_to_float     __floatsisf
  167. #        define float_to_si     __fixsfsi
  168. #        define float_to_usi     __fixunssfsi
  169. #        define negate         __negsf2
  170. #        define sf_to_df        __extendsfdf2
  171. #else
  172. #        define add         __adddf3
  173. #        define sub         __subdf3
  174. #        define multiply     __muldf3
  175. #        define divide         __divdf3
  176. #        define compare         __cmpdf2
  177. #        define _eq_f2         __eqdf2
  178. #        define _ne_f2         __nedf2
  179. #        define _gt_f2         __gtdf2
  180. #        define _ge_f2         __gedf2
  181. #        define _lt_f2         __ltdf2
  182. #        define _le_f2         __ledf2
  183. #        define si_to_float     __floatsidf
  184. #        define float_to_si     __fixdfsi
  185. #        define float_to_usi     __fixunsdfsi
  186. #        define negate         __negdf2
  187. #        define df_to_sf        __truncdfsf2
  188. #    endif
  189. #endif
  190.  
  191.  
  192. #define INLINE __inline__
  193.  
  194. /* Preserve the sticky-bit when shifting fractions to the right.  */
  195. #define LSHIFT(a) { a = (a & 1) | (a >> 1); }
  196.  
  197. /* numeric parameters */
  198. /* F_D_BITOFF is the number of bits offset between the MSB of the mantissa
  199.    of a float and of a double. Assumes there are only two float types.
  200.    (double::FRAC_BITS+double::NGARGS-(float::FRAC_BITS-float::NGARDS))
  201.  */
  202. #define F_D_BITOFF (52+8-(23+7))
  203.  
  204.  
  205. #define NORMAL_EXPMIN (-(EXPBIAS)+1)
  206. #define IMPLICIT_1 (1LL<<(FRACBITS+NGARDS))
  207. #define IMPLICIT_2 (1LL<<(FRACBITS+1+NGARDS))
  208.  
  209. /* common types */
  210.  
  211. typedef enum
  212. {
  213.   CLASS_SNAN,
  214.   CLASS_QNAN,
  215.   CLASS_ZERO,
  216.   CLASS_NUMBER,
  217.   CLASS_INFINITY
  218. } fp_class_type;
  219.  
  220. typedef struct
  221. {
  222. #ifdef SMALL_MACHINE
  223.   char class;
  224.   unsigned char sign;
  225.   short normal_exp;
  226. #else
  227.   fp_class_type class;
  228.   unsigned int sign;
  229.   int normal_exp;
  230. #endif
  231.  
  232.   union
  233.     {
  234.       fractype ll;
  235.       halffractype l[2];
  236.     } fraction;
  237. } fp_number_type;
  238.  
  239. typedef union
  240. {
  241.   FLO_type value;
  242. #ifdef _DEBUG_BITFLOAT
  243.   int l[2];
  244. #endif
  245.   struct
  246.     {
  247. #ifndef FLOAT_BIT_ORDER_MISMATCH
  248.       unsigned int sign:1 __attribute__ ((packed));
  249.       unsigned int exp:EXPBITS __attribute__ ((packed));
  250.       fractype fraction:FRACBITS __attribute__ ((packed));
  251. #else
  252.       fractype fraction:FRACBITS __attribute__ ((packed));
  253.       unsigned int exp:EXPBITS __attribute__ ((packed));
  254.       unsigned int sign:1 __attribute__ ((packed));
  255. #endif
  256.     }
  257.   bits;
  258. }
  259. FLO_union_type;
  260.  
  261.  
  262. /* end of header */
  263.  
  264. /* IEEE "special" number predicates */
  265.  
  266. #ifdef NO_NANS
  267.  
  268. #define nan() 0
  269. #define isnan(x) 0
  270. #define isinf(x) 0
  271. #else
  272.  
  273. INLINE
  274. static fp_number_type *
  275. nan ()
  276. {
  277.   static fp_number_type thenan;
  278.  
  279.   return &thenan;
  280. }
  281.  
  282. INLINE
  283. static int
  284. isnan ( fp_number_type *  x)
  285. {
  286.   return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
  287. }
  288.  
  289. INLINE
  290. static int
  291. isinf ( fp_number_type *  x)
  292. {
  293.   return x->class == CLASS_INFINITY;
  294. }
  295.  
  296. #endif
  297.  
  298. INLINE
  299. static int
  300. iszero ( fp_number_type *  x)
  301. {
  302.   return x->class == CLASS_ZERO;
  303. }
  304.  
  305. INLINE 
  306. static void
  307. flip_sign ( fp_number_type *  x)
  308. {
  309.   x->sign = !x->sign;
  310. }
  311.  
  312. static FLO_type
  313. pack_d ( fp_number_type *  src)
  314. {
  315.   FLO_union_type dst;
  316.   fractype fraction = src->fraction.ll;    /* wasn't unsigned before? */
  317.  
  318.   dst.bits.sign = src->sign;
  319.  
  320.   if (isnan (src))
  321.     {
  322.       dst.bits.exp = EXPMAX;
  323.       dst.bits.fraction = src->fraction.ll;
  324.       if (src->class == CLASS_QNAN || 1)
  325.     {
  326.       dst.bits.fraction |= QUIET_NAN;
  327.     }
  328.     }
  329.   else if (isinf (src))
  330.     {
  331.       dst.bits.exp = EXPMAX;
  332.       dst.bits.fraction = 0;
  333.     }
  334.   else if (iszero (src))
  335.     {
  336.       dst.bits.exp = 0;
  337.       dst.bits.fraction = 0;
  338.     }
  339.   else if (fraction == 0)
  340.     {
  341.       dst.value = 0;
  342.     }
  343.   else
  344.     {
  345.       if (src->normal_exp < NORMAL_EXPMIN)
  346.     {
  347.       /* This number's exponent is too low to fit into the bits
  348.          available in the number, so we'll store 0 in the exponent and
  349.          shift the fraction to the right to make up for it.  */
  350.  
  351.       int shift = NORMAL_EXPMIN - src->normal_exp;
  352.  
  353.       dst.bits.exp = 0;
  354.  
  355.       if (shift > FRAC_NBITS - NGARDS)
  356.         {
  357.           /* No point shifting, since it's more that 64 out.  */
  358.           fraction = 0;
  359.         }
  360.       else
  361.         {
  362.           /* Shift by the value */
  363.           fraction >>= shift;
  364.         }
  365.       fraction >>= NGARDS;
  366.       dst.bits.fraction = fraction;
  367.     }
  368.       else if (src->normal_exp > EXPBIAS)
  369.     {
  370.       dst.bits.exp = EXPMAX;
  371.       dst.bits.fraction = 0;
  372.     }
  373.       else
  374.     {
  375.       dst.bits.exp = src->normal_exp + EXPBIAS;
  376.       /* IF the gard bits are the all zero, but the first, then we're
  377.          half way between two numbers, choose the one which makes the
  378.          lsb of the answer 0.  */
  379.       if ((fraction & GARDMASK) == GARDMSB)
  380.         {
  381.           if (fraction & (1 << NGARDS))
  382.         fraction += GARDROUND + 1;
  383.         }
  384.       else
  385.         {
  386.           /* Add a one to the guards to round up */
  387.           fraction += GARDROUND;
  388.         }
  389.       if (fraction >= IMPLICIT_2)
  390.         {
  391.           fraction >>= 1;
  392.           dst.bits.exp += 1;
  393.         }
  394.       fraction >>= NGARDS;
  395.       dst.bits.fraction = fraction;
  396.     }
  397.     }
  398.   return dst.value;
  399. }
  400.  
  401. static void
  402. unpack_d (FLO_union_type * src, fp_number_type * dst)
  403. {
  404.   fractype fraction = src->bits.fraction;
  405.  
  406.   dst->sign = src->bits.sign;
  407.   if (src->bits.exp == 0)
  408.     {
  409.       /* Hmm.  Looks like 0 */
  410.       if (fraction == 0)
  411.     {
  412.       /* tastes like zero */
  413.       dst->class = CLASS_ZERO;
  414.     }
  415.       else
  416.     {
  417.       /* Zero exponent with non zero fraction - it's denormalized,
  418.          so there isn't a leading implicit one - we'll shift it so
  419.          it gets one.  */
  420.       dst->normal_exp = src->bits.exp - EXPBIAS + 1;
  421.       fraction <<= NGARDS;
  422.  
  423.       dst->class = CLASS_NUMBER;
  424. #if 1
  425.       while (fraction < IMPLICIT_1)
  426.         {
  427.           fraction <<= 1;
  428.           dst->normal_exp--;
  429.         }
  430. #endif
  431.       dst->fraction.ll = fraction;
  432.     }
  433.     }
  434.   else if (src->bits.exp == EXPMAX)
  435.     {
  436.       /* Huge exponent*/
  437.       if (fraction == 0)
  438.     {
  439.       /* Attached to a zero fraction - means infinity */
  440.       dst->class = CLASS_INFINITY;
  441.     }
  442.       else
  443.     {
  444.       /* Non zero fraction, means nan */
  445.       if (dst->sign)
  446.         {
  447.           dst->class = CLASS_SNAN;
  448.         }
  449.       else
  450.         {
  451.           dst->class = CLASS_QNAN;
  452.         }
  453.       /* Keep the fraction part as the nan number */
  454.       dst->fraction.ll = fraction;
  455.     }
  456.     }
  457.   else
  458.     {
  459.       /* Nothing strange about this number */
  460.       dst->normal_exp = src->bits.exp - EXPBIAS;
  461.       dst->class = CLASS_NUMBER;
  462.       dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
  463.     }
  464. }
  465.  
  466. static fp_number_type *
  467. _fpadd_parts (fp_number_type * a,
  468.           fp_number_type * b,
  469.           fp_number_type * tmp)
  470. {
  471.   intfrac tfraction;
  472.  
  473.   /* Put commonly used fields in local variables.  */
  474.   int a_normal_exp;
  475.   int b_normal_exp;
  476.   fractype a_fraction;
  477.   fractype b_fraction;
  478.  
  479.   if (isnan (a))
  480.     {
  481.       return a;
  482.     }
  483.   if (isnan (b))
  484.     {
  485.       return b;
  486.     }
  487.   if (isinf (a))
  488.     {
  489.       /* Adding infinities with opposite signs yields a NaN.  */
  490.       if (isinf (b) && a->sign != b->sign)
  491.     return nan ();
  492.       return a;
  493.     }
  494.   if (isinf (b))
  495.     {
  496.       return b;
  497.     }
  498.   if (iszero (b))
  499.     {
  500.       return a;
  501.     }
  502.   if (iszero (a))
  503.     {
  504.       return b;
  505.     }
  506.  
  507.   /* Got two numbers. shift the smaller and increment the exponent till
  508.      they're the same */
  509.   {
  510.     int diff;
  511.  
  512.     a_normal_exp = a->normal_exp;
  513.     b_normal_exp = b->normal_exp;
  514.     a_fraction = a->fraction.ll;
  515.     b_fraction = b->fraction.ll;
  516.  
  517.     diff = a_normal_exp - b_normal_exp;
  518.  
  519.     if (diff < 0)
  520.       diff = -diff;
  521.     if (diff < FRAC_NBITS)
  522.       {
  523.     /* ??? This does shifts one bit at a time.  Optimize.  */
  524.     while (a_normal_exp > b_normal_exp)
  525.       {
  526.         b_normal_exp++;
  527.         LSHIFT (b_fraction);
  528.       }
  529.     while (b_normal_exp > a_normal_exp)
  530.       {
  531.         a_normal_exp++;
  532.         LSHIFT (a_fraction);
  533.       }
  534.       }
  535.     else
  536.       {
  537.     /* Somethings's up.. choose the biggest */
  538.     if (a_normal_exp > b_normal_exp)
  539.       {
  540.         b_normal_exp = a_normal_exp;
  541.         b_fraction = 0;
  542.       }
  543.     else
  544.       {
  545.         a_normal_exp = b_normal_exp;
  546.         a_fraction = 0;
  547.       }
  548.       }
  549.   }
  550.  
  551.   if (a->sign != b->sign)
  552.     {
  553.       if (a->sign)
  554.     {
  555.       tfraction = -a_fraction + b_fraction;
  556.     }
  557.       else
  558.     {
  559.       tfraction = a_fraction - b_fraction;
  560.     }
  561.       if (tfraction > 0)
  562.     {
  563.       tmp->sign = 0;
  564.       tmp->normal_exp = a_normal_exp;
  565.       tmp->fraction.ll = tfraction;
  566.     }
  567.       else
  568.     {
  569.       tmp->sign = 1;
  570.       tmp->normal_exp = a_normal_exp;
  571.       tmp->fraction.ll = -tfraction;
  572.     }
  573.       /* and renormalize it */
  574.  
  575.       while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
  576.     {
  577.       tmp->fraction.ll <<= 1;
  578.       tmp->normal_exp--;
  579.     }
  580.     }
  581.   else
  582.     {
  583.       tmp->sign = a->sign;
  584.       tmp->normal_exp = a_normal_exp;
  585.       tmp->fraction.ll = a_fraction + b_fraction;
  586.     }
  587.   tmp->class = CLASS_NUMBER;
  588.   /* Now the fraction is added, we have to shift down to renormalize the
  589.      number */
  590.  
  591.   if (tmp->fraction.ll >= IMPLICIT_2)
  592.     {
  593.       LSHIFT (tmp->fraction.ll);
  594.       tmp->normal_exp++;
  595.     }
  596.   return tmp;
  597.  
  598. }
  599.  
  600. FLO_type
  601. add (FLO_type arg_a, FLO_type arg_b)
  602. {
  603.   fp_number_type a;
  604.   fp_number_type b;
  605.   fp_number_type tmp;
  606.   fp_number_type *res;
  607.  
  608.   unpack_d ((FLO_union_type *) & arg_a, &a);
  609.   unpack_d ((FLO_union_type *) & arg_b, &b);
  610.  
  611.   res = _fpadd_parts (&a, &b, &tmp);
  612.  
  613.   return pack_d (res);
  614. }
  615.  
  616. FLO_type
  617. sub (FLO_type arg_a, FLO_type arg_b)
  618. {
  619.   fp_number_type a;
  620.   fp_number_type b;
  621.   fp_number_type tmp;
  622.   fp_number_type *res;
  623.  
  624.   unpack_d ((FLO_union_type *) & arg_a, &a);
  625.   unpack_d ((FLO_union_type *) & arg_b, &b);
  626.  
  627.   b.sign ^= 1;
  628.  
  629.   res = _fpadd_parts (&a, &b, &tmp);
  630.  
  631.   return pack_d (res);
  632. }
  633.  
  634. static fp_number_type *
  635. _fpmul_parts ( fp_number_type *  a,
  636.            fp_number_type *  b,
  637.            fp_number_type * tmp)
  638. {
  639.   fractype low = 0;
  640.   fractype high = 0;
  641.  
  642.   if (isnan (a))
  643.     {
  644.       a->sign = a->sign != b->sign;
  645.       return a;
  646.     }
  647.   if (isnan (b))
  648.     {
  649.       b->sign = a->sign != b->sign;
  650.       return b;
  651.     }
  652.   if (isinf (a))
  653.     {
  654.       if (iszero (b))
  655.     return nan ();
  656.       a->sign = a->sign != b->sign;
  657.       return a;
  658.     }
  659.   if (isinf (b))
  660.     {
  661.       if (iszero (a))
  662.     {
  663.       return nan ();
  664.     }
  665.       b->sign = a->sign != b->sign;
  666.       return b;
  667.     }
  668.   if (iszero (a))
  669.     {
  670.       a->sign = a->sign != b->sign;
  671.       return a;
  672.     }
  673.   if (iszero (b))
  674.     {
  675.       b->sign = a->sign != b->sign;
  676.       return b;
  677.     }
  678.  
  679.   /* Calculate the mantissa by multiplying both 64bit numbers to get a
  680.      128 bit number */
  681.   {
  682.     fractype x = a->fraction.ll;
  683.     fractype ylow = b->fraction.ll;
  684.     fractype yhigh = 0;
  685.     int bit;
  686.  
  687. #if defined(NO_DI_MODE)
  688.     {
  689.       /* ??? This does multiplies one bit at a time.  Optimize.  */
  690.       for (bit = 0; bit < FRAC_NBITS; bit++)
  691.     {
  692.       int carry;
  693.  
  694.       if (x & 1)
  695.         {
  696.           carry = (low += ylow) < ylow;
  697.           high += yhigh + carry;
  698.         }
  699.       yhigh <<= 1;
  700.       if (ylow & FRACHIGH)
  701.         {
  702.           yhigh |= 1;
  703.         }
  704.       ylow <<= 1;
  705.       x >>= 1;
  706.     }
  707.     }
  708. #elif defined(FLOAT) 
  709.     {
  710.       /* Multiplying two 32 bit numbers to get a 64 bit number  on 
  711.         a machine with DI, so we're safe */
  712.  
  713.       DItype answer = (DItype)(a->fraction.ll) * (DItype)(b->fraction.ll);
  714.       
  715.       high = answer >> 32;
  716.       low = answer;
  717.     }
  718. #else
  719.     /* Doing a 64*64 to 128 */
  720.     {
  721.       UDItype nl = a->fraction.ll & 0xffffffff;
  722.       UDItype nh = a->fraction.ll >> 32;
  723.       UDItype ml = b->fraction.ll & 0xffffffff;
  724.       UDItype mh = b->fraction.ll >>32;
  725.       UDItype pp_ll = ml * nl;
  726.       UDItype pp_hl = mh * nl;
  727.       UDItype pp_lh = ml * nh;
  728.       UDItype pp_hh = mh * nh;
  729.       UDItype res2 = 0;
  730.       UDItype res0 = 0;
  731.       UDItype ps_hh__ = pp_hl + pp_lh;
  732.       if (ps_hh__ < pp_hl)
  733.     res2 += 0x100000000LL;
  734.       pp_hl = (ps_hh__ << 32) & 0xffffffff00000000LL;
  735.       res0 = pp_ll + pp_hl;
  736.       if (res0 < pp_ll)
  737.     res2++;
  738.       res2 += ((ps_hh__ >> 32) & 0xffffffffL) + pp_hh;
  739.       high = res2;
  740.       low = res0;
  741.     }
  742. #endif
  743.   }
  744.  
  745.   tmp->normal_exp = a->normal_exp + b->normal_exp;
  746.   tmp->sign = a->sign != b->sign;
  747. #ifdef FLOAT
  748.   tmp->normal_exp += 2;        /* ??????????????? */
  749. #else
  750.   tmp->normal_exp += 4;        /* ??????????????? */
  751. #endif
  752.   while (high >= IMPLICIT_2)
  753.     {
  754.       tmp->normal_exp++;
  755.       if (high & 1)
  756.     {
  757.       low >>= 1;
  758.       low |= FRACHIGH;
  759.     }
  760.       high >>= 1;
  761.     }
  762.   while (high < IMPLICIT_1)
  763.     {
  764.       tmp->normal_exp--;
  765.  
  766.       high <<= 1;
  767.       if (low & FRACHIGH)
  768.     high |= 1;
  769.       low <<= 1;
  770.     }
  771.   /* rounding is tricky. if we only round if it won't make us round later. */
  772. #if 0
  773.   if (low & FRACHIGH2)
  774.     {
  775.       if (((high & GARDMASK) != GARDMSB)
  776.       && (((high + 1) & GARDMASK) == GARDMSB))
  777.     {
  778.       /* don't round, it gets done again later. */
  779.     }
  780.       else
  781.     {
  782.       high++;
  783.     }
  784.     }
  785. #endif
  786.   if ((high & GARDMASK) == GARDMSB)
  787.     {
  788.       if (high & (1 << NGARDS))
  789.     {
  790.       /* half way, so round to even */
  791.       high += GARDROUND + 1;
  792.     }
  793.       else if (low)
  794.     {
  795.       /* but we really weren't half way */
  796.       high += GARDROUND + 1;
  797.     }
  798.     }
  799.   tmp->fraction.ll = high;
  800.   tmp->class = CLASS_NUMBER;
  801.   return tmp;
  802. }
  803.  
  804. FLO_type
  805. multiply (FLO_type arg_a, FLO_type arg_b)
  806. {
  807.   fp_number_type a;
  808.   fp_number_type b;
  809.   fp_number_type tmp;
  810.   fp_number_type *res;
  811.  
  812.   unpack_d ((FLO_union_type *) & arg_a, &a);
  813.   unpack_d ((FLO_union_type *) & arg_b, &b);
  814.  
  815.   res = _fpmul_parts (&a, &b, &tmp);
  816.  
  817.   return pack_d (res);
  818. }
  819.  
  820. static fp_number_type *
  821. _fpdiv_parts (fp_number_type * a,
  822.           fp_number_type * b,
  823.           fp_number_type * tmp)
  824. {
  825.   fractype low = 0;
  826.   fractype high = 0;
  827.   fractype r0, r1, y0, y1, bit;
  828.   fractype q;
  829.   fractype numerator;
  830.   fractype denominator;
  831.   fractype quotient;
  832.   fractype remainder;
  833.  
  834.   if (isnan (a))
  835.     {
  836.       return a;
  837.     }
  838.   if (isnan (b))
  839.     {
  840.       return b;
  841.     }
  842.   if (isinf (a) || iszero (a))
  843.     {
  844.       if (a->class == b->class)
  845.     return nan ();
  846.       return a;
  847.     }
  848.   a->sign = a->sign ^ b->sign;
  849.  
  850.   if (isinf (b))
  851.     {
  852.       a->fraction.ll = 0;
  853.       a->normal_exp = 0;
  854.       return a;
  855.     }
  856.   if (iszero (b))
  857.     {
  858.       a->class = CLASS_INFINITY;
  859.       return b;
  860.     }
  861.  
  862.   /* Calculate the mantissa by multiplying both 64bit numbers to get a
  863.      128 bit number */
  864.   {
  865.     int carry;
  866.     intfrac d0, d1;        /* weren't unsigned before ??? */
  867.  
  868.     /* quotient =
  869.        ( numerator / denominator) * 2^(numerator exponent -  denominator exponent)
  870.      */
  871.  
  872.     a->normal_exp = a->normal_exp - b->normal_exp;
  873.     numerator = a->fraction.ll;
  874.     denominator = b->fraction.ll;
  875.  
  876.     if (numerator < denominator)
  877.       {
  878.     /* Fraction will be less than 1.0 */
  879.     numerator *= 2;
  880.     a->normal_exp--;
  881.       }
  882.     bit = IMPLICIT_1;
  883.     quotient = 0;
  884.     /* ??? Does divide one bit at a time.  Optimize.  */
  885.     while (bit)
  886.       {
  887.     if (numerator >= denominator)
  888.       {
  889.         quotient |= bit;
  890.         numerator -= denominator;
  891.       }
  892.     bit >>= 1;
  893.     numerator *= 2;
  894.       }
  895.  
  896.     if ((quotient & GARDMASK) == GARDMSB)
  897.       {
  898.     if (quotient & (1 << NGARDS))
  899.       {
  900.         /* half way, so round to even */
  901.         quotient += GARDROUND + 1;
  902.       }
  903.     else if (numerator)
  904.       {
  905.         /* but we really weren't half way, more bits exist */
  906.         quotient += GARDROUND + 1;
  907.       }
  908.       }
  909.  
  910.     a->fraction.ll = quotient;
  911.     return (a);
  912.   }
  913. }
  914.  
  915. FLO_type
  916. divide (FLO_type arg_a, FLO_type arg_b)
  917. {
  918.   fp_number_type a;
  919.   fp_number_type b;
  920.   fp_number_type tmp;
  921.   fp_number_type *res;
  922.  
  923.   unpack_d ((FLO_union_type *) & arg_a, &a);
  924.   unpack_d ((FLO_union_type *) & arg_b, &b);
  925.  
  926.   res = _fpdiv_parts (&a, &b, &tmp);
  927.  
  928.   return pack_d (res);
  929. }
  930.  
  931. /* according to the demo, fpcmp returns a comparison with 0... thus
  932.    a<b -> -1
  933.    a==b -> 0
  934.    a>b -> +1
  935.  */
  936.  
  937. static int
  938. _fpcmp_parts (fp_number_type * a, fp_number_type * b)
  939. {
  940. #if 0
  941.   /* either nan -> unordered. Must be checked outside of this routine. */
  942.   if (isnan (a) && isnan (b))
  943.     {
  944.       return 1;            /* still unordered! */
  945.     }
  946. #endif
  947.  
  948.   if (isnan (a) || isnan (b))
  949.     {
  950.       return 1;            /* how to indicate unordered compare? */
  951.     }
  952.   if (isinf (a) && isinf (b))
  953.     {
  954.       /* +inf > -inf, but +inf != +inf */
  955.       /* b    \a| +inf(0)| -inf(1)
  956.        ______\+--------+--------
  957.        +inf(0)| a==b(0)| a<b(-1)
  958.        -------+--------+--------
  959.        -inf(1)| a>b(1) | a==b(0)
  960.        -------+--------+--------
  961.        So since unordered must be non zero, just line up the columns...
  962.        */
  963.       return b->sign - a->sign;
  964.     }
  965.   /* but not both... */
  966.   if (isinf (a))
  967.     {
  968.       return a->sign ? -1 : 1;
  969.     }
  970.   if (isinf (b))
  971.     {
  972.       return b->sign ? 1 : -1;
  973.     }
  974.   if (iszero (a) && iszero (b))
  975.     {
  976.       return 0;
  977.     }
  978.   if (iszero (a))
  979.     {
  980.       return b->sign ? 1 : -1;
  981.     }
  982.   if (iszero (b))
  983.     {
  984.       return a->sign ? -1 : 1;
  985.     }
  986.   /* now both are "normal". */
  987.   if (a->sign != b->sign)
  988.     {
  989.       /* opposite signs */
  990.       return a->sign ? -1 : 1;
  991.     }
  992.   /* same sign; exponents? */
  993.   if (a->normal_exp > b->normal_exp)
  994.     {
  995.       return a->sign ? -1 : 1;
  996.     }
  997.   if (a->normal_exp < b->normal_exp)
  998.     {
  999.       return a->sign ? 1 : -1;
  1000.     }
  1001.   /* same exponents; check size. */
  1002.   if (a->fraction.ll > b->fraction.ll)
  1003.     {
  1004.       return a->sign ? -1 : 1;
  1005.     }
  1006.   if (a->fraction.ll < b->fraction.ll)
  1007.     {
  1008.       return a->sign ? 1 : -1;
  1009.     }
  1010.   /* after all that, they're equal. */
  1011.   return 0;
  1012. }
  1013.  
  1014. CMPtype
  1015. compare (FLO_type arg_a, FLO_type arg_b)
  1016. {
  1017.   fp_number_type a;
  1018.   fp_number_type b;
  1019.  
  1020.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1021.   unpack_d ((FLO_union_type *) & arg_b, &b);
  1022.  
  1023.   return _fpcmp_parts (&a, &b);
  1024. }
  1025.  
  1026. #ifndef US_SOFTWARE_GOFAST
  1027.  
  1028. /* These should be optimized for their specific tasks someday.  */
  1029.  
  1030. CMPtype
  1031. _eq_f2 (FLO_type arg_a, FLO_type arg_b)
  1032. {
  1033.   fp_number_type a;
  1034.   fp_number_type b;
  1035.  
  1036.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1037.   unpack_d ((FLO_union_type *) & arg_b, &b);
  1038.  
  1039.   if (isnan (&a) || isnan (&b))
  1040.     return 1;            /* false, truth == 0 */
  1041.  
  1042.   return _fpcmp_parts (&a, &b) ;
  1043. }
  1044.  
  1045. CMPtype
  1046. _ne_f2 (FLO_type arg_a, FLO_type arg_b)
  1047. {
  1048.   fp_number_type a;
  1049.   fp_number_type b;
  1050.  
  1051.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1052.   unpack_d ((FLO_union_type *) & arg_b, &b);
  1053.  
  1054.   if (isnan (&a) || isnan (&b))
  1055.     return 1;            /* true, truth != 0 */
  1056.  
  1057.   return  _fpcmp_parts (&a, &b) ;
  1058. }
  1059.  
  1060. CMPtype
  1061. _gt_f2 (FLO_type arg_a, FLO_type arg_b)
  1062. {
  1063.   fp_number_type a;
  1064.   fp_number_type b;
  1065.  
  1066.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1067.   unpack_d ((FLO_union_type *) & arg_b, &b);
  1068.  
  1069.   if (isnan (&a) || isnan (&b))
  1070.     return -1;            /* false, truth > 0 */
  1071.  
  1072.   return _fpcmp_parts (&a, &b);
  1073. }
  1074.  
  1075. CMPtype
  1076. _ge_f2 (FLO_type arg_a, FLO_type arg_b)
  1077. {
  1078.   fp_number_type a;
  1079.   fp_number_type b;
  1080.  
  1081.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1082.   unpack_d ((FLO_union_type *) & arg_b, &b);
  1083.  
  1084.   if (isnan (&a) || isnan (&b))
  1085.     return -1;            /* false, truth >= 0 */
  1086.   return _fpcmp_parts (&a, &b) ;
  1087. }
  1088.  
  1089. CMPtype
  1090. _lt_f2 (FLO_type arg_a, FLO_type arg_b)
  1091. {
  1092.   fp_number_type a;
  1093.   fp_number_type b;
  1094.  
  1095.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1096.   unpack_d ((FLO_union_type *) & arg_b, &b);
  1097.  
  1098.   if (isnan (&a) || isnan (&b))
  1099.     return 1;            /* false, truth < 0 */
  1100.  
  1101.   return _fpcmp_parts (&a, &b);
  1102. }
  1103.  
  1104. CMPtype
  1105. _le_f2 (FLO_type arg_a, FLO_type arg_b)
  1106. {
  1107.   fp_number_type a;
  1108.   fp_number_type b;
  1109.  
  1110.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1111.   unpack_d ((FLO_union_type *) & arg_b, &b);
  1112.  
  1113.   if (isnan (&a) || isnan (&b))
  1114.     return 1;            /* false, truth <= 0 */
  1115.  
  1116.   return _fpcmp_parts (&a, &b) ;
  1117. }
  1118.  
  1119. #endif /* ! US_SOFTWARE_GOFAST */
  1120.  
  1121. FLO_type
  1122. si_to_float (SItype arg_a)
  1123. {
  1124.   fp_number_type in;
  1125.  
  1126.   in.class = CLASS_NUMBER;
  1127.   in.sign = arg_a < 0;
  1128.   if (!arg_a)
  1129.     {
  1130.       in.class = CLASS_ZERO;
  1131.     }
  1132.   else
  1133.     {
  1134.       in.normal_exp = FRACBITS + NGARDS;
  1135.       if (in.sign) 
  1136.     {
  1137.       /* Special case for minint, since there is no +ve integer
  1138.          representation for it */
  1139.       if (arg_a == 0x80000000)
  1140.         {
  1141.           return -2147483648.0;
  1142.         }
  1143.       in.fraction.ll = (-arg_a);
  1144.     }
  1145.       else
  1146.     in.fraction.ll = arg_a;
  1147.  
  1148.       while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
  1149.     {
  1150.       in.fraction.ll <<= 1;
  1151.       in.normal_exp -= 1;
  1152.     }
  1153.     }
  1154.   return pack_d (&in);
  1155. }
  1156.  
  1157. SItype
  1158. float_to_si (FLO_type arg_a)
  1159. {
  1160.   fp_number_type a;
  1161.   SItype tmp;
  1162.  
  1163.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1164.   if (iszero (&a))
  1165.     return 0;
  1166.   if (isnan (&a))
  1167.     return 0;
  1168.   /* get reasonable MAX_SI_INT... */
  1169.   if (isinf (&a))
  1170.     return a.sign ? MAX_SI_INT : (-MAX_SI_INT)-1;
  1171.   /* it is a number, but a small one */
  1172.   if (a.normal_exp < 0)
  1173.     return 0;
  1174.   if (a.normal_exp > 30)
  1175.     return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
  1176.   tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
  1177.   return a.sign ? (-tmp) : (tmp);
  1178. }
  1179.  
  1180. #ifdef US_SOFTWARE_GOFAST
  1181. /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
  1182.    we also define them for GOFAST because the ones in libgcc2.c have the
  1183.    wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
  1184.    out of libgcc2.c.  We can't define these here if not GOFAST because then
  1185.    there'd be duplicate copies.  */
  1186.  
  1187. USItype
  1188. float_to_usi (FLO_type arg_a)
  1189. {
  1190.   fp_number_type a;
  1191.  
  1192.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1193.   if (iszero (&a))
  1194.     return 0;
  1195.   if (isnan (&a))
  1196.     return 0;
  1197.   /* get reasonable MAX_USI_INT... */
  1198.   if (isinf (&a))
  1199.     return a.sign ? MAX_USI_INT : 0;
  1200.   /* it is a negative number */
  1201.   if (a.sign)
  1202.     return 0;
  1203.   /* it is a number, but a small one */
  1204.   if (a.normal_exp < 0)
  1205.     return 0;
  1206.   if (a.normal_exp > 31)
  1207.     return MAX_USI_INT;
  1208.   else if (a.normal_exp > (FRACBITS + NGARDS))
  1209.     return a.fraction.ll << ((FRACBITS + NGARDS) - a.normal_exp);
  1210.   else
  1211.     return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
  1212. }
  1213. #endif
  1214.  
  1215. FLO_type
  1216. negate (FLO_type arg_a)
  1217. {
  1218.   fp_number_type a;
  1219.  
  1220.   unpack_d ((FLO_union_type *) & arg_a, &a);
  1221.   flip_sign (&a);
  1222.   return pack_d (&a);
  1223. }
  1224.  
  1225. #ifdef FLOAT
  1226.  
  1227. SFtype
  1228. __make_fp(fp_class_type class,
  1229.          unsigned int sign,
  1230.          int exp, 
  1231.          USItype frac)
  1232. {
  1233.   fp_number_type in;
  1234.  
  1235.   in.class = class;
  1236.   in.sign = sign;
  1237.   in.normal_exp = exp;
  1238.   in.fraction.ll = frac;
  1239.   return pack_d (&in);
  1240. }
  1241.  
  1242. #ifndef FLOAT_ONLY
  1243.  
  1244. /* This enables one to build an fp library that supports float but not double.
  1245.    Otherwise, we would get an undefined reference to __make_dp.
  1246.    This is needed for some 8-bit ports that can't handle well values that
  1247.    are 8-bytes in size, so we just don't support double for them at all.  */
  1248.  
  1249. extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype frac);
  1250.  
  1251. DFtype
  1252. sf_to_df (SFtype arg_a)
  1253. {
  1254.   fp_number_type in;
  1255.  
  1256.   unpack_d ((FLO_union_type *) & arg_a, &in);
  1257.   return __make_dp (in.class, in.sign, in.normal_exp,
  1258.             ((UDItype) in.fraction.ll) << F_D_BITOFF);
  1259. }
  1260.  
  1261. #endif
  1262. #endif
  1263.  
  1264. #ifndef FLOAT
  1265.  
  1266. extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
  1267.  
  1268. DFtype
  1269. __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
  1270. {
  1271.   fp_number_type in;
  1272.  
  1273.   in.class = class;
  1274.   in.sign = sign;
  1275.   in.normal_exp = exp;
  1276.   in.fraction.ll = frac;
  1277.   return pack_d (&in);
  1278. }
  1279.  
  1280. SFtype
  1281. df_to_sf (DFtype arg_a)
  1282. {
  1283.   fp_number_type in;
  1284.  
  1285.   unpack_d ((FLO_union_type *) & arg_a, &in);
  1286.   return __make_fp (in.class, in.sign, in.normal_exp,
  1287.             in.fraction.ll >> F_D_BITOFF);
  1288. }
  1289.  
  1290. #endif
  1291.